<?php
/**
 * Created by PhpStorm.
 * User: qiaoli
 * Date: 15/11/12
 * Time: 下午5:18
 */
class Postjfm extends CI_Model
{
    function __construct()
    {
        // 初始化并继承cache
        parent::__construct();
        $this->load->model("userm");
        $this->load->model("postm");
        $this->load->model("commentm");
        $this->load->model("newjfpointsm");
        $this->load->model("invitem");

    }
    function post_jf_add_bak($post_id)
    {
        $post_count = $this->postm->post_count_today_user($this->uid);
        if($post_count <= 5 )//每天最多5帖给分
        {
            $user_info = $this->userm->getUserInfoById($this->uid);

            //取消积分职称梯度，首次发帖实名15积分，非实名10积分，以后每次发帖都是实名10积分，非实名5积分   2016 - 02 - 25  李晓阳

            $is_first_post = 0;
            if($post_count == 1)
            {
                //判断是否是第一次发帖
                if($this->postm->is_first_post($this->uid))
                {
                    $is_first_post = 1;
                }
            }

            //如果是第一次发帖 走第一次发帖的流程 否则 走平时的流程
            if($is_first_post)
            {
                if($user_info['is_show_truename'])
                {
                    $jf_add = 15;
                    $msg_content = "[user:".$this->uid.": ]".'您实名完成了首次发帖，获得了15个积分';
                }
                else
                {
                    $jf_add = 10;
                    $msg_content = "[user:".$this->uid.": ]".'您完成了首次发帖，获得了10个积分';
                }
            }
            else
            {
                if($user_info['is_show_truename'])
                {
                    $jf_add = 10;
                    $msg_content = "[user:".$this->uid.": ]".'您实名发布了一张新帖，获得了10个积分';
                }
                else
                {
                    $jf_add = 5;
                    $msg_content = "[user:".$this->uid.": ]".'您发布了一张新帖，获得了5个积分';
                }
            }



            $this->jfpointsm->post_jfpoint_normal($this->uid,$post_id,$jf_add);


            $this->load->mode('messagem');
            $this->messagem->send_message($post_id,$this->uid,$msg_content, $this->uid);

            /*
            $post_title = $user_info['post_title'];
            if(!is_numeric($post_title))
            {
                $post_title = $this->verifiedm->transform_post_title_id($post_title);
            }
            w_log('$post_title = '.$post_title);
            //6 || 11 || 16  付主任
            //7 || 12  || 17  主任
            //副主任以上实名   每帖30积分,非实名20积分
            if($post_title == 6 || $post_title == 11 || $post_title == 16 || $post_title == 7 || $post_title == 12 || $post_title == 17)
            {
                if($user_info['is_show_truename'])
                {
                    $jf_add = 30;
                    $msg_content = "[user:".$this->uid.": ]".'您实名发布了一张新帖，获得了30个积分';
                }
                else
                {
                    $jf_add = 20;
                    $msg_content = "[user:".$this->uid.": ]".'您发布了一张新帖，获得了20个积分';
                }
            }
            else if($post_title == 5 || $post_title == 10 || $post_title == 15)//主治、主管实名20 积分 非实名10
            {
                if($user_info['is_show_truename'])
                {
                    $jf_add = 20;
                    $msg_content = "[user:".$this->uid.": ]".'您实名发布了一张新帖，获得了20个积分';
                }
                else
                {
                    $jf_add = 10;
                    $msg_content = "[user:".$this->uid.": ]".'您发布了一张新帖，获得了10个积分';
                }
            }
            else if(in_array($post_title,array(3,4,8,9,13,14,18,19,20,21,22,23)) && $user_info['is_show_truename']) //住院实名10 积分
            {
                $jf_add = 10;
                $msg_content = "[user:".$this->uid.": ]".'您实名发布了一张新帖，获得了10个积分';
            }
            else
            {
                if($post_count == 1)
                {
                    //判断是否是第一次发帖
                    if($this->postm->is_first_post($this->uid))
                    {
                        $this->jfpointsm->post_jfpoint_normal($this->uid,$post_id,10);
                        $msg_content = "[user:".$this->uid.": ]".'您完成了首次发帖，获得了10个积分';
                        $this->messagem->send_message($post_id,$this->uid,$msg_content);
                        return;
                    }
                }

                $jf_add = 5;
                $msg_content = "[user:".$this->uid.": ]".'您发布了一张新帖，获得了5个积分';
            }
            $this->jfpointsm->post_jfpoint_normal($this->uid,$post_id,$jf_add);


            $this->messagem->send_message($post_id,$this->uid,$msg_content);*/
        }
    }

    function post_jf_add($post_id, $post_type_extend,$author_id)
    {
        //分享贴
        if($post_type_extend == 1)
        {
            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_SHARE_POST,$post_id,Newjfpointsm::ADD_TYPE);
        }
        //求助贴
        elseif($post_type_extend == 3)
        {
            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_QUICK_POST,$post_id,Newjfpointsm::ADD_TYPE);
        }
//        //问答贴
//        elseif($post_type_extend == 4)
//        {
//            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_ASK_ANSWER,$post_id,Newjfpointsm::ADD_TYPE);
//        }
//        //投票贴
//        elseif($post_type_extend == 5)
//        {
//            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_VOTE,$post_id,Newjfpointsm::ADD_TYPE);
//        }
    }
    function post_jf_del($post_id,$author_id)
    {
        //发布完整分享病例
        $info = $this->postm->get_post_info($post_id);
        //分享贴
        if($info['post_type_extend'] == 1)
        {
            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_SHARE_POST,$post_id,Newjfpointsm::MINUS_TYPE);
        }
        //求助贴
        elseif($info['post_type_extend'] == 3)
        {
            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_QUICK_POST,$post_id,Newjfpointsm::MINUS_TYPE);
        }
//        //问答贴
//        elseif($info['post_type_extend'] == 4)
//        {
//            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_ASK_ANSWER,$post_id,Newjfpointsm::MINUS_TYPE);
//        }
//        //投票贴
//        elseif($info['post_type_extend'] == 5)
//        {
//            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,DAILY_PUBLISH_VOTE,$post_id,Newjfpointsm::MINUS_TYPE);
//        }
        //如果是品牌贴
        w_log('$info[\'is_pinpai\'] is '.$info['is_pinpai']);
        if($info['is_pinpai'] == 2)
        {
            w_log('pinpai tie 去除积分');
            $this->newjfpointsm->add_minusjf_sendmessage($author_id,$post_id,POST_TOBE_PINPAI,$post_id,Newjfpointsm::MINUS_TYPE);
        }
    }

    /**
     * 开启实名，评论添加积分
     * 50字以上评论给予积分  非实名 1积分  实名 2积分  2016-02-25 李晓阳
     */
    function comment_jf_add($comment_id,$comment_info,$post_info,$observe_id)
    {
        $post_id = $comment_info['post_id'];
        $comment_author_id = $comment_info['author_id'];
        $post_comment_count = $this->commentm->get_count_for_client($post_id,$cache=0);
        $this->load->model('newjfpointsm');

        $message = $comment_info['message'];
        //回答任意病例(字数超过30字)  病例和问答 字数超过30个字   投票超过10个字  才有积分
        if(iconv_strlen($message,"UTF-8") >= 10 && !in_array($post_info['post_type_extend'],array(4,5)) && $post_info['post_type'] != 1) {
            //$this->jfpointsm->insert_comment_jfpoint($this->uid, $post_id,$post_info['post_type_extend']);
            //不再判断是否为沙发
            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_POST_COMMENT_NO_SOFA,$comment_id,Newjfpointsm::ADD_TYPE);

        }
        //问答积分 改成 30个字就给分
        elseif(iconv_strlen($message,"UTF-8") >= 30 && $post_info['post_type_extend'] == 4)
        {
            //不再判断是否为沙发
            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_QUESTION_COMMENT_NO_SOFA,$comment_id,Newjfpointsm::ADD_TYPE);
        }
        elseif(iconv_strlen($message,"UTF-8") >= 30 && $post_info['post_type_extend'] == 5)
        {

            //$this->jfpointsm->insert_comment_jfpoint($this->uid, $post_id,$post_info['post_type_extend']);
            //不判断是否为沙发  投票
            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_VOTE_COMMENT,$comment_id,Newjfpointsm::ADD_TYPE);
        }

        //以下积分  跟帖子有关 以帖子为related_id   是给发帖人积分
        $unique_comment_user_count = $this->commentm->get_unique_comment_user_count($post_id);  //获取帖子评论人数

//        if($unique_comment_user_count == 5 && $post_info['post_type_extend'] == 5)
//        {
//            //w_log('问答准备加分');
//            $this->newjfpointsm->add_minusjf_sendmessage($post_info['author_id'],$post_id,COMMENT_VOTE_OVER_FIVE,$comment_id,Newjfpointsm::ADD_TYPE);
//
//        }

        //以下积分  跟帖子有关 以帖子为related_id
        if($unique_comment_user_count == 5 && $post_info['post_type_extend'] == 4)
        {
            $this->newjfpointsm->add_minusjf_sendmessage($post_info['author_id'],$post_id,COMMENT_QUESTION_OVER_FIVE,$comment_id,Newjfpointsm::ADD_TYPE);
        }
    }


    /**
     * 删除评论处理积分
     * @param $comment_id 评论id
     * @param $comment_info
     * @param $post_info
     * @param int $observe_id  app使用者
     */
    public function comment_jf_del($comment_id,$comment_info,$post_info,$observe_id)
    {
        //w_log('开始处理评论积分');
        //获取评论发布时间
        $post_id = $comment_info['post_id'];
        $comment_author_id = $comment_info['author_id'];
        if (!$observe_id
            || ($comment_author_id != $observe_id)
        ) {
            w_log('只要不是自己删除自己的评论都要直接扣6分');
            w_log('$observe_id is '.$observe_id);
            w_log('$comment_author_id is '.$comment_author_id);
            //这里要用加分  减分的话 会判断是否加过 会出错
            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DEL_COMMENT,$comment_id,Newjfpointsm::ADD_TYPE);
        }
        //自己删除自己的评论 如果改扣多少扣多少
        elseif ($comment_author_id == $observe_id)
        {
            $message = $comment_info['message'];
            //回答任意病例(字数超过30字)  病例和问答 字数超过30个字   投票超过10个字  才有积分
            if (iconv_strlen($message,"UTF-8") >= 10
                && !in_array($post_info['post_type_extend'],array(4,5))
                && $post_info['post_type'] != 1
            ) {
                //$this->jfpointsm->insert_comment_jfpoint($this->uid, $post_id,$post_info['post_type_extend']);
                //不再判断是否为沙发
                $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_POST_COMMENT_NO_SOFA,$comment_id,Newjfpointsm::MINUS_TYPE);

            }
            //问答积分 改成 30个字就给分
            elseif (iconv_strlen($message,"UTF-8") >= 30
                && $post_info['post_type_extend'] == 4
            ) {
                //不再判断是否为沙发
                $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_QUESTION_COMMENT_NO_SOFA,$comment_id,Newjfpointsm::MINUS_TYPE);
            }
            elseif (iconv_strlen($message,"UTF-8") >= 30
                && $post_info['post_type_extend'] == 5
            ) {

                //$this->jfpointsm->insert_comment_jfpoint($this->uid, $post_id,$post_info['post_type_extend']);
                //不判断是否为沙发  投票
                $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_VOTE_COMMENT,$comment_id,Newjfpointsm::MINUS_TYPE);
            }
        }
//        //后台没有$this->uid  后台或版主删除评论 先扣2积分再说
//        if(!$observe_id || ($comment_author_id != $observe_id))
//        {
//            //w_log('$observe_id is '.$observe_id);
//            //w_log('$comment_author_id is '.$comment_author_id);
//            //这里要用加分  减分的话 会判断是否加过 会出错
//            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DEL_COMMENT,$comment_id,Newjfpointsm::ADD_TYPE);
//        }
//
//        //获取这个帖子的评论数  以下提到的病例 不包括专家咨询帖
//        $post_comment_count = $this->commentm->get_count_for_client($post_id,$cache=0);
//        //回答任意病例沙发(字数超过30字)  病例和问答 字数超过10个字   投票超过10个字  才有积分
//        w_log(iconv_strlen($comment_info['message'],"UTF-8"));
//        if(iconv_strlen($comment_info['message'],"UTF-8") >= 10 && !in_array($post_info['post_type_extend'],array(4,5)) && $post_info['post_type'] != 1) {
//
//            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_POST_COMMENT_NO_SOFA,$comment_id,Newjfpointsm::MINUS_TYPE);
//
//        }
//        //问答和投票都是评论字数超过10个
//        elseif(iconv_strlen($comment_info['message'],"UTF-8") >= 30 && $post_info['post_type_extend'] == 4)
//        {
//            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_QUESTION_COMMENT_NO_SOFA,$comment_id,Newjfpointsm::MINUS_TYPE);
//        }
//        elseif(iconv_strlen($comment_info['message'],"UTF-8") >= 30 && $post_info['post_type_extend'] == 5)
//        {
//            //不判断是否为沙发  投票
//            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,DAILY_VOTE_COMMENT,$comment_id,Newjfpointsm::MINUS_TYPE);
//
//        }
//


        //w_log('$observe_id is '.$observe_id);
        //w_log('$comment_author_id is '.$comment_author_id);

        //如果是答案 需要去掉 答案相关的积分
        if($comment_info['is_answer'])
        {
            $this->newjfpointsm->invite_jf_change($post_info['post_type_extend'],$comment_author_id,$post_id,Newjfpointsm::MINUS_TYPE);
        }

        //以下积分  跟帖子有关 以帖子为related_id  给发帖人减积分  如果人数不够了 扣分 够的话 不扣分
        $unique_comment_user_count = $this->commentm->get_unique_comment_user_count($post_id);  //获取帖子评论人数

        if($unique_comment_user_count == 5 && $post_info['post_type_extend'] == 4)
        {
            //w_log('问答准备加分');
            $this->newjfpointsm->add_minusjf_sendmessage($post_info['author_id'],$post_id,COMMENT_VOTE_OVER_FIVE,$comment_id,Newjfpointsm::MINUS_TYPE);

        }

        // 发起过邀请的人
        $post_info = $this->postm->get_post_info($comment_info["post_id"]);
        $post_type_extend = $post_info["post_type_extend"];
        # 评论成功后  如果有人邀请我 那么有效回答 则给分 每天十积分  被删除时 不减分
        $invite_send_uid = $this->invitem->invite_send_uid_not_answer($post_id,$comment_author_id);
        //只要回答就给分 不再判断是否为有效邀请
        //if($invite_send_uid && iconv_strlen($message,"UTF-8") >= 10)
        if($invite_send_uid)
        {
            $this->load->model('newjfpointsm');
            if($post_type_extend == 4)
            {
                $jf_relative_type1 = QUESTION_INVITED_COMMENT;
                $jf_relative_type2 = QUESTION_INVITED_COMMENT_SUCCESS;
            }
            else
            {
                $jf_relative_type1 = INVITED_COMMENT;
                $jf_relative_type2 = INVITED_COMMENT_SUCCESS;
            }
            $this->newjfpointsm->add_minusjf_sendmessage($comment_author_id,$post_id,$jf_relative_type1,$post_id,Newjfpointsm::MINUS_TYPE);


            //更新邀请状态 变为已回答 避免重复给分
            //$this->invitem->update_invite_status($post_id,$author_id);
            //邀请人也有积分
            foreach($invite_send_uid as $invite_me_id)
            {
                //
                $this->newjfpointsm->add_minusjf_sendmessage($invite_me_id,$post_id,$jf_relative_type2,$post_id,Newjfpointsm::MINUS_TYPE);

            }
        }



        //以下积分  跟帖子有关 以帖子为related_id
        //投票评论人数 不再给积分
        //if($unique_comment_user_count == 5 && $post_info['post_type_extend'] == 4)
        //{
            //$this->newjfpointsm->add_minusjf_sendmessage($post_info['author_id'],$post_id,COMMENT_QUESTION_OVER_FIVE,$comment_id,Newjfpointsm::MINUS_TYPE);
        //}
    }
}